package cn.xopencode.securityservice.service.violate.impl;

import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.securityservice.convert.activity.ActivityConvert;
import cn.xopencode.securityservice.convert.violate.ViolateConvert;
import cn.xopencode.securityservice.dal.mysql.dataobject.activity.ActivityDO;
import cn.xopencode.securityservice.dal.mysql.dataobject.violate.ViolateDO;
import cn.xopencode.securityservice.dal.mysql.mapper.activity.ActivityMapper;
import cn.xopencode.securityservice.dal.mysql.mapper.violate.ViolateMapper;
import cn.xopencode.securityservice.enums.violate.ViolateAuditStatusEnum;
import cn.xopencode.securityservice.rpc.activity.dto.ActivityCreateReqDTO;
import cn.xopencode.securityservice.rpc.activity.dto.ActivityPageReqDTO;
import cn.xopencode.securityservice.rpc.activity.dto.ActivityRespDTO;
import cn.xopencode.securityservice.rpc.violate.dto.ViolateAuditReqDTO;
import cn.xopencode.securityservice.rpc.violate.dto.ViolateCreateReqDTO;
import cn.xopencode.securityservice.rpc.violate.dto.ViolatePageReqDTO;
import cn.xopencode.securityservice.rpc.violate.dto.ViolateRespDTO;
import cn.xopencode.securityservice.service.activity.ActivityService;
import cn.xopencode.securityservice.service.violate.ViolateService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;

/**
 * 反三违及事故情况 Service 实现
 */
@Service
public class ViolateServiceImpl implements ViolateService {
    @Autowired
    private ViolateMapper violateMapper;

    @Override
    public Integer createViolate(ViolateCreateReqDTO createDTO) {
        ViolateDO violateDO = ViolateConvert.INSTANCE.convert(createDTO);
        violateDO.setId(IdUtil.objectId());
        violateDO.setCreateTime(new Date());
        violateDO.setAuditStatus(ViolateAuditStatusEnum.WAIT.getStatus());
        return violateMapper.insert(violateDO);

    }

    @Override
    public PageResult<ViolateRespDTO> pageViolate(ViolatePageReqDTO pageReqDTO) {
        IPage<ViolateDO> violateDO = violateMapper.selectPage(pageReqDTO.setSorts(Collections.singletonList(ViolatePageReqDTO.FINDTIME_DESC)));
        return ViolateConvert.INSTANCE.convertPage(violateDO);
    }

    @Override
    public ViolateRespDTO getViolate(String violateId) {
        ViolateDO violateDO = violateMapper.selectById(violateId);
        return ViolateConvert.INSTANCE.convert(violateDO);
    }

    @Override
    public Integer auditViolate(ViolateAuditReqDTO reqDTO) {
        ViolateDO violateDO = ViolateConvert.INSTANCE.convert(reqDTO);
        violateDO.setAuditTime(new Date());
        return violateMapper.updateById(violateDO);
    }

}

